public class 有效的括号 {

    //https://leetcode.cn/problems/valid-parentheses/?envType=study-plan-v2&envId=top-interview-150
    //栈的使用基础
    public boolean isValid(String s) {
        //手动实现栈
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < s.length();i++){
            //左括号加
            char x = s.charAt(i);
            if(x == '(' ||x == '{' ||x == '['){
                sb.append(x);
            }else{
                //右括号先判空
                if(sb.length() == 0){
                    return false;
                }
                //看是否匹配, 匹配就删掉匹配的括号
                char ch = sb.charAt(sb.length() - 1);
                if((x == ')' &&  ch =='(')
                        || (x == '}' && ch =='{')
                        || (x == ']' && ch =='[')){
                    sb.deleteCharAt(sb.length() - 1);
                }else{
                    return false;
                }
            }
        }
        //栈中有括号直接拜拜
        return sb.length()== 0 ? true : false;
    }
}
